$(document).ready(function () {

    $("#addTaskButton").click(function () {
        showTask("");
    });
    $("#exportTaskButton").click(function () {
    	var sels = selectTaskIds();
    	if(sels){
    		window.location.href = basePath+"pub/exportTaskListXML.do?selects=" + sels;
    	}
    });
    
    $("#checkboxAll").click(function () {
        $(".taskIdClass").attr("checked", this.checked);
    });

    $("#delTaskButton").click(function () {
        var taskIds = selectTaskIds();
        if(taskIds){
            del(taskIds);
        }
    });

    $("#copyTaskButton").click(function () {
        var taskIds = selectTaskIds();
        if(taskIds){
            showCopyTask(taskIds);
        }
    });

    $("#enableDisableButton").click(function () {
        var taskIds = selectTaskIds();
        if(taskIds){
            enableDisable(taskIds);
        }
    });
    $("#immediatelyRunButton").click(function () {
        var taskIds = selectTaskIds();
        if (taskIds) {
            immediatelyRun(taskIds);
        }
    });
    $("#batchModifyButton").click(function () {
        var taskIds = selectTaskIds();
        if (taskIds) {
            showBatchModifyTask(taskIds);
        }
    });
    $("#interruptButton").click(function () {
        var taskIds = selectTaskIds();
        if (taskIds) {
            interruptJob(taskIds);
        }
    });
});

function del(taskId) {
    if (confirm("确定删除任务吗？")) {
        $.post(
            basePath+"pub/delTask.do",
            {
                "taskIds": taskId
            },
            function (data) {

                if (data.jsonMap.status == "success") {
                    alert("删除任务成功");
                    refreshWindow();
                } else {
                    alert(data.jsonMap.status);
                }
            }
        );
    }
}
function copyTaskSubmit() {

    var taskIds = $("#copy_taskIds").val();
    var count = $("#copy_count").val();
    var jobNode = $("#copy_jobNode").val();
    var taskNamePrefix = $("#copy_taskNamePrefix").val();
    var serial = $("input[name='copy_serial']:checked").val();

    if (count == undefined || count == '' || isNaN(count)) {
        alert("请输入数量");
    } else {
        $.post(
            basePath+"pub/copyTask.do",
            {
                "taskIds": taskIds,
                "copyCount":count,
                "taskNamePrefix":taskNamePrefix,
                "jobNode":jobNode,
                "serial":serial
            },
            function (data) {

                if (data.jsonMap.status == "success") {
                    alert("复制任务成功");
                    refreshWindow();
                } else {
                    alert(data.jsonMap.status);
                }
            }
        );

    }
}
function enableDisable(taskIds) {

    if (confirm("确定启用/停用任务吗？")) {
        $.post(
            basePath+"pub/enableDisableTask.do",
            {
                "taskIds": taskIds
            },
            function (data) {

                if (data.jsonMap.status == "success") {
                    alert("启用/停用任务成功");
                    refreshWindow();
                } else {
                    alert(data.jsonMap.status);
                }
            }
        );

    }
}
function interruptJob(taskIds) {

    if (confirm("确定中断任务吗？")) {
        $.post(
            basePath + "pub/interrupt.do",
            {
                "taskIds": taskIds
            },
            function (data) {

                if (data.jsonMap.status == "success") {
                    alert("中断任务成功");
                    refreshWindow();
                } else {
                    alert(data.jsonMap.status);
                }
            }
        );

    }
}
function immediatelyRun(taskIds) {

    if (confirm("确定立即运行任务吗？")) {
        $.post(
            basePath + "pub/immediatelyRunTask.do",
            {
                "taskIds": taskIds
            },
            function (data) {

                if (data.jsonMap.status == "success") {
                    alert("立即运行任务成功");
                    refreshWindow();
                } else {
                    alert(data.jsonMap.status);
                }
            }
        );

    }
}
function showCopyTask(taskIds) {

    $('#copy_task_div').showWindow({
            title: '复制任务信息',
            width: 400,
            height: 250,
            data: {
                "taskIds": taskIds
            }
        }
    );
}
function showBatchModifyTask(taskIds) {

    $('#batch_modify_task_div').showWindow({
            title: '批量更新任务信息',
            width: 860,
            height: 400,
            data: {
                "taskIds": taskIds
            }
        }
    );
}
function showTask(taskId) {

    $('#task_div').showWindow({
            title: '任务信息',
            width: 860,
            height: 600,
            data: {
                "taskId": taskId
            }
        }
    );
}
function showTaskJobLogList(taskId) {

    $('#task_job_log_div').showWindow({
            title: '日志列表',
            width: 1000,
            height: 650,
            data: {
                "taskId": taskId
            }
        }
    );
}
function showTaskJobLogInfo(taskJobLogId) {
    $('#task_job_log_info_div').showWindow({
            title: '日志明细',
            width: 800,
            height: 520,
            data: {
                "taskJobLog.taskJobLogId": taskJobLogId
            }
        }
    );
}
function showTaskOperationLogList(taskId) {
    $('#task_operation_log_div').showWindow({
            title: '操作日志',
            width: 850,
            height: 510,
            data: {
                "taskId": taskId
            }
        }
    );
}
function selectTaskIds(){
    var taskIds = "";

    $("input[class='taskIdClass']:checkbox:checked").each(function () {
        taskIds += this.value + ",";
    });

    if (taskIds.length == 0) {
        alert("请选择任务");
        return null;
    }else{
        taskIds = taskIds.substr(0, taskIds.length - 1);
        return taskIds;
    }
}
function querySubmit(){
    var taskId = $.trim($("#taskId").val());

    if (taskId != "") {
        var type = "^[0-9]*[1-9][0-9]*$";
        var re = new RegExp(type);
        if (taskId.match(re) == null) {
            alert("“任务ID”应为正整数!");
            $("#taskId").val("");
            $("#taskId").focus();
            return false;
        }
    }

    return true;
}
function refreshWindow() {
    $("#query_form").submit();
}
function refreshQuery() {
    if ($("#refreshQuery").attr("checked")) {
        refreshWindow();
    }
}
function sortQuery(sort, sortField) {
    $("#sort").val(sort);
    $("#sortField").val(sortField);
    $("#query_form").submit();
}
setInterval('refreshQuery()',5000);
